var fs = require("fs");
var iconv = require('iconv-lite');
var mongoose = require("mongoose");
db=mongoose.connect("mongodb://admin:tbs@127.0.0.1:27017/weixin")
// mongoose.connect("mongodb://127.0.0.1:27017/testData");
mongoose.Promise = require('bluebird');

var db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection error:'));

db.once('open', function() {
    console.log("we're connected!");
});

var timeStart = Date.now();

var Ranking = mongoose.Schema({
    studentId: String,
    datesOfAttendance: String,
    studentName: String,
    college: String,
    major: String,
    class: String,
    persentOfMajor: String,
    english: String,
    makeupExamination: String,
    physical: String,
    GPA: String,
    rankingInCollage: String,
    numberOfCollage: String,
    highestGPAInCollage: String,
    lowestGPAInCollage: String,
    rankingInMajor: String,
    numberOfMajor: String,
    highestGPAInMajor: String,
    lowestGPAInMajor: String,
    rankingInClass: String,
    numberOfClass: String,
    highestGPAInClass: String,
    lowestGPAInClass: String
});

var Detail = db.model('ranking', Ranking);


fs.readFile('./data.csv', (err, data) => {
    var count = 0;
    if (err) throw err;
    var str = iconv.decode(data, 'gb2312');
    var array = str.split("\n");


    console.log(array.length);
    //递归调用存储
    saveData(array, 1, array.length);

});




function saveData(array, index, totall) {

    if (index == totall-1) {
        var timeEnd = Date.now();
        console.log("saved " + totall + " students used " + (timeEnd - timeStart) + " ms");
        return;
    } else {
        var studentInfo = array[index];
        studentInfo = studentInfo.replace("\r", "");
        var studentInfoArray = studentInfo.split(",");
        var stuDetail = new Detail({
            studentId: studentInfoArray[0],
            datesOfAttendance: studentInfoArray[1],
            studentName: studentInfoArray[2],
            college: studentInfoArray[3],
            major: studentInfoArray[4],
            class: studentInfoArray[5],
            persentOfMajor: studentInfoArray[6],
            english: studentInfoArray[7],
            makeupExamination: studentInfoArray[8],
            physical: studentInfoArray[9],
            GPA: studentInfoArray[10],
            rankingInCollage: studentInfoArray[11],
            numberOfCollage: studentInfoArray[12],
            highestGPAInCollage: studentInfoArray[13],
            lowestGPAInCollage: studentInfoArray[14],
            rankingInMajor: studentInfoArray[15],
            numberOfMajor: studentInfoArray[16],
            highestGPAInMajor: studentInfoArray[17],
            lowestGPAInMajor: studentInfoArray[18],
            rankingInClass: studentInfoArray[19],
            numberOfClass: studentInfoArray[20],
            highestGPAInClass: studentInfoArray[21],
            lowestGPAInClass: studentInfoArray[22]
        });
        stuDetail.save(function() {
            index++;
            saveData(array, index, totall);
        })

    }
}
